package com.tfswx.nginxcleanhc.api;

import com.tfswx.nginxcleanhc.util.CmdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author ricky
 * @date 2022/10/13
 */
@RestController
@RequestMapping
public class ApiController {

    @Value("${cache-path}")
    private String cachePath;

    @GetMapping("cleanHc")
    public String api(String key) throws IOException, InterruptedException {
        if (!StringUtils.hasText(key)) {
            throw new RuntimeException("key-不能为空");
        }
        String cmd = "sh nginx_cache_purge \"" + key + "\" " + cachePath;

        List<String> strings = CmdUtil.execToList(cmd);

        return String.join("\r\n", strings);
    }
}
